﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Automation;
using Guia.Core;
using NUnit.Framework;
using Guia.Elements;
using WpfTestApplication;

namespace NUnitTests{
    /// <summary>
    /// Summary description for WindowControlTest
    /// </summary>
    [TestFixture]
    public class WindowControlTest : AbstractTest
    {
        private WindowControl windowControl;

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [SetUp()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        [TearDown()]
        public void MyTestCleanup()
        {
            if (this.windowControl != null)
            {
                this.windowControl.Stop();
            }
        }
        //
        #endregion

        [Test]
        public void TestStart()
        {
            windowControl = WindowControl.Start<ApplicationTestWindow>();
            Assert.IsNotNull(windowControl);
        }

        [Test]
        public void TestStartWithConstructorParameter()
        {
            windowControl = WindowControl.Start<ApplicationTestWindow>("Show this","And this");
            Assert.IsNotNull(windowControl);

            //check constructor
            Label label1 = windowControl.Get<Label>("label1");
            Assert.AreEqual("Show this", label1.TextValue);
            Label label2 = windowControl.Get<Label>("label2");
            Assert.AreEqual("And this", label2.TextValue);
        }

        // TODO: Invalid Constructor

        // TODO: Maximize

        // TODO: Minimize

        // TODO: RecoverNormal

        // TODO: Close

        // TODO: GetMenu

        [Test, ExpectedException(typeof(OperationNotSupportedException))]
        public void TestMaximizeNotAllowed()
        {
            WindowControl windowControl2 = null;
            try
            {
                windowControl2 = WindowControl.Start<WindowTestWindow2>();
                Assert.IsNotNull(windowControl2);
                windowControl2.Maximize();
            }
            finally
            {
                if (windowControl2 != null)
                {
                    windowControl2.Stop();
                }
            }
        }

        [Test]
        public void TestMaximizedWindow()
        {
            windowControl = WindowControl.Start<ApplicationTestWindow>();
            Assert.IsNotNull(windowControl);
            windowControl.Maximize();
            Assert.AreEqual(WindowVisualState.Maximized, windowControl.GetVisualState());
        }

        [Test]
        public void TestRecoverMaximized()
        {
            windowControl = WindowControl.Start<ApplicationTestWindow>();
            Assert.IsNotNull(windowControl);
            windowControl.Maximize();
            Assert.AreEqual(WindowVisualState.Maximized, windowControl.GetVisualState());
            // now, recover to normal
            windowControl.RecoverNormal();
            Assert.AreEqual(WindowVisualState.Normal, windowControl.GetVisualState());
        }

        [Test]
        public void TestMinimize()
        {
            windowControl = WindowControl.Start<ApplicationTestWindow>();
            Assert.IsNotNull(windowControl);
            windowControl.Minimize();
            Assert.AreEqual(WindowVisualState.Minimized, windowControl.GetVisualState());
        }

        [Test, ExpectedException(typeof(OperationNotSupportedException))]
        public void TestMinimizeNotAllowed()
        {
            WindowControl windowControl2 = null;
            try
            {
                windowControl2 = WindowControl.Start<WindowTestWindow2>();
                Assert.IsNotNull(windowControl2);
                windowControl2.Minimize();
            }
            finally
            {
                if(windowControl2 != null)
                {
                    windowControl2.Stop();
                }
            }
        }
    }
}
